package com.jike.noobmoney.adapter;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;


import com.image.imagepicker.ImagePicker;
import com.image.imagepicker.ui.ImageGridActivity;
import com.jike.noobmoney.R;
import com.jike.noobmoney.contants.ConstantValue;
import com.jike.noobmoney.entity.MyPushDetailEntity;
import com.jike.noobmoney.helper.ImageLoader;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

public class StepAdapter extends BaseAdapter {

	Context context;
	LayoutInflater layoutInflater;
	ArrayList<MyPushDetailEntity.TaskstepBean> productList;
	OnCartClick  onCartClick;
	//public int foodpoition;

	public StepAdapter(Context context, ArrayList<MyPushDetailEntity.TaskstepBean> productList) {
		this.context = context;
		this.productList = productList;
		layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	}

	public void setOnCartClick(OnCartClick onCartClick) {
		this.onCartClick = onCartClick;
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return productList.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return productList.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		ViewHolder viewHolder = null;
		final int location=position;
		if (convertView == null) {
			convertView = layoutInflater.inflate(R.layout.item_add_layout, null);
			viewHolder = new ViewHolder();
			viewHolder.et_text=(EditText)convertView.findViewById(R.id.et_text);
			viewHolder.iv_pic=(ImageView)convertView.findViewById(R.id.iv_pic);
			viewHolder.cb_check=(CheckBox)convertView.findViewById(R.id.cb_check);
			viewHolder.tv_remove_view=(TextView) convertView.findViewById(R.id.tv_remove_view);
			viewHolder.tv_add_view=(TextView)convertView.findViewById(R.id.tv_add_view);
			convertView.setTag(viewHolder);
		} else {
			viewHolder = (ViewHolder) convertView.getTag();
		}
		MyPushDetailEntity.TaskstepBean  productModel=productList.get(position);
		if(productModel==null){
			viewHolder.et_text.setText("");
			ImageLoader.displayImage(context, ConstantValue.IMG_BASEURL_MARK +"", viewHolder.iv_pic);
			viewHolder.tv_remove_view.setOnClickListener(new View.OnClickListener() {
				@Override
				public void onClick(View view) {
					onCartClick.delete(productModel);
				}
			});
			viewHolder.tv_add_view.setOnClickListener(new View.OnClickListener() {
				@Override
				public void onClick(View view) {
					onCartClick.add();
				}
			});
			viewHolder.iv_pic.setOnClickListener(new View.OnClickListener() {
				@Override
				public void onClick(View view) {
//				ImagePicker.getInstance().setSelectLimit(1);
//				ImagePicker.getInstance().setCrop(false);
//				ImagePicker.getInstance().setShowCamera(true);
//				ImagePicker.getInstance().setMultiMode(true);
//				Intent intent1 = new Intent(context, ImageGridActivity.class);
//				((Activity)context).startActivityForResult(intent1, 1110);
					onCartClick.addPic(productModel,position);
				}
			});
			viewHolder.iv_pic.setTag("");
		}else
		{
			//        通过tag判断当前editText是否已经设置监听，有监听的话，移除监听再给editText赋值
			if (viewHolder.et_text.getTag() instanceof TextWatcher){
				viewHolder.et_text.removeTextChangedListener((TextWatcher) viewHolder.et_text.getTag());
			}
//        必须在判断tag后给editText赋值，否则会数据错乱
			viewHolder.et_text.setText(productModel.getText()+"");
			TextWatcher watcher = new TextWatcher() {
				@Override
				public void beforeTextChanged(CharSequence s, int start, int count, int after) {

				}

				@Override
				public void onTextChanged(CharSequence s, int start, int before, int count) {

				}

				@Override
				public void afterTextChanged(Editable editable) {
					if (!TextUtils.isEmpty(editable)) {
						productModel.setText(String.valueOf(editable + ""));
					}
				}
			};
//        给item中的editText设置监听
			viewHolder.et_text.addTextChangedListener(watcher);
//        给editText设置tag，以便于判断当前editText是否已经设置监听
			viewHolder.et_text.setTag(watcher);

			if(productModel.getPicture()==null||productModel.getPicture().length()==0){
				ImageLoader.displayImage(context, R.drawable.image_add_nor, viewHolder.iv_pic);
			}else {
				ImageLoader.displayImage(context, ConstantValue.IMG_BASEURL_MARK + productModel.getPicture(), viewHolder.iv_pic);
			}
			viewHolder.tv_remove_view.setOnClickListener(new View.OnClickListener() {
				@Override
				public void onClick(View view) {
					onCartClick.delete(productModel);
				}
			});
			viewHolder.tv_add_view.setOnClickListener(new View.OnClickListener() {
				@Override
				public void onClick(View view) {
					onCartClick.add();
				}
			});
			viewHolder.iv_pic.setOnClickListener(new View.OnClickListener() {
				@Override
				public void onClick(View view) {
//				ImagePicker.getInstance().setSelectLimit(1);
//				ImagePicker.getInstance().setCrop(false);
//				ImagePicker.getInstance().setShowCamera(true);
//				ImagePicker.getInstance().setMultiMode(true);
//				Intent intent1 = new Intent(context, ImageGridActivity.class);
//				((Activity)context).startActivityForResult(intent1, 1110);
					onCartClick.addPic(productModel,position);
				}
			});
			viewHolder.iv_pic.setTag(productModel.getPicture());
			if(productModel.getFlag()==0){
				viewHolder.cb_check.setChecked(false);
			}else{
				viewHolder.cb_check.setChecked(true);
			}
		}

		return convertView;
	}

	public static class ViewHolder {
		public EditText et_text;
		public ImageView iv_pic;
		public CheckBox cb_check;
		public TextView tv_remove_view;
		public TextView tv_add_view;
 	}
	public interface OnCartClick{
			public void delete(MyPushDetailEntity.TaskstepBean bean);
			public void add();
			public void addPic(MyPushDetailEntity.TaskstepBean bean,int positon);
	}
	public void setList(ArrayList<MyPushDetailEntity.TaskstepBean> list) {
		this.productList = list;
		notifyDataSetChanged();
	}
}
